var app = getApp();
console.log(app)
const host = app.globalData.host

let subscribers = [];
function onAccessTokenFetched() {
	subscribers.forEach((callback) => {
		callback(); // 此回调会执行接口then后面的逻辑
	})
	subscribers = [];
}
function addSubscriber(callback) {
	subscribers.push(callback)
}
export class Http {
	constructor() {}
	// 请求方法
	request(options) {
		const _this = this;
		const {
			url,
			data = {},
			header={
        'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
      },
			loading = true,
			method = 'GET',
			callback = '',
			goon = true, // 队列后是否继续执行后面操作
		} = options;
		data.token=wx.getStorageSync('token');
    data.clientId=1003
    header.token=wx.getStorageSync('token');
    header.clientId=1003
    header.version='1.0.0'
    header.timestamp=new Date().getTime()
		// 开启加载中
		// loading && wx.showLoading({
		// 	title: '加载中',
		// 	mask: true
		// });
		// 返回一个promise
		return new Promise((resolve, reject) => {
			wx.request({
				url:host + url,
				data,
				method: method.toLocaleUpperCase(),
				header: header,
				success: (res) => {
					console.log(res)
						// 关闭加载中
						loading && wx.hideLoading();
						const {
							statusCode
						} = res;
						const responseData = res.data
						if (callback) return callback(res.data);
						if (statusCode == 200) {
              // 验证接口状态码
              
							if (responseData.code == 200) {
								resolve(responseData)
							} else if (responseData.code == 0) {
                // reject(responseData)
                console.log(1122)
								// 展示错误
                wx.showToast({
                  title:responseData.msg||'请求超时',
                  icon: 'none',
                  duration: 4000,
                })
							} else if (responseData.code == 403) {
                // 将需要重新执行的接口缓存到一个队列中
                console.log(1133)
								if(goon) {
									addSubscriber(() => {
										_this.request({
											url,
											data,
											loading,
											method,
											callback: resolve
										})
									})
								}
                wx.showToast({
                  title:responseData.msg||'请求超时',
                  icon: 'none',
                  duration: 4000,
                })
								wx.removeStorageSync('token')
								wx.removeStorageSync('time')
							} else {
                console.log(1144,responseData)
                // 展示错误
                wx.showToast({
                  title: responseData.msg||'请求超时',
                  icon:'none'
                })
                // wx.showToast({
                //   content:responseData.msg||'请求超时',
                //   icon: 'none',
                //   duration: 4000,
                // })
							}
						} else if(statusCode==403){
							if(goon) {
								addSubscriber(() => {
									_this.request({
										url,
										data,
										loading,
										method,
										callback: resolve
									})
								})
							}
              wx.showToast({
                title:responseData.msg||'请求超时',
                icon: 'none',
                duration: 4000,
              })
							return false
						}else {
							wx.showToast({
								title: '提示',
                icon: 'none',
                duration: 4000,
							})
							reject()
					  }
					
				},
				fail: (err) => {
					reject(err);
					// 关闭加载中
					loading && wx.hideLoading();
				}
			})
		})
	}
}
